//
//  FFBaseTabBar.swift
//  SwiftWeiBo
//
//  Created by 乔庆芳 on 16/4/10.
//  Copyright © 2016年 乔庆芳. All rights reserved.
//

import UIKit

class FFBaseTabBar: UITabBar {

    //定义闭包
    var closure:(()->())?
    
    override func layoutSubviews() {
        //一定要写super.layoutSubViews
        super.layoutSubviews()
        //按钮的个数
        let count=5
        //按钮的宽度
        let width=self.frame.size.width/CGFloat(count)
        
        var index=0
        //遍历
        for button in self.subviews{
        
            if button.isKindOfClass(NSClassFromString("UITabBarButton")!){
            //设置frame
                button.frame=CGRectMake(CGFloat(index)*width, 0, width, frame.size.height)
            
                
                if index>1{
                
                    button.frame=CGRectMake(CGFloat(index+1)*width, 0, width, frame.size.height)
                }
                index++
                
            }
            
        }
        sumButton.frame=CGRectMake(2*width, 0, width, frame.size.height)
        
    }
    
    //加号的点击事件
    @objc private func sumClick(){
    
        //调用闭包
        closure?()
    }
    //懒加载加号按钮
    private lazy var sumButton : UIButton = {
    
        let sumBT=UIButton()
        
        //设置图片
        sumBT.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        sumBT.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        sumBT.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        sumBT.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        sumBT.addTarget(self, action:  Selector("sumClick"), forControlEvents: UIControlEvents.TouchUpInside)
        //添加到tabBar上
        
        self.addSubview(sumBT)
        return  sumBT
    }()

}
